package com.hui.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hui.mapper.SystemGroupDataMapper;
import com.hui.entity.dao.SystemGroupData;
import com.hui.entity.dto.SystemGroupDataQueryCriteria;
import com.hui.entity.enums.CommonEnum;
import com.hui.service.SystemGroupDataService;
import com.hui.util.ResponseResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合数据详情表(SystemGroupData)表服务实现类
 *
 * @author hui
 * @since 2022-07-12 16:46:17
 */
@Service("systemGroupDataService")
public class SystemGroupDataServiceImpl extends ServiceImpl<SystemGroupDataMapper, SystemGroupData> implements SystemGroupDataService {

    /**
     * 获取首页轮播图
     * 获取四个小菜单
     *
     * @return
     */
    @Override
    public ResponseResult getDatas(String name) {
        LambdaQueryWrapper<SystemGroupData> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SystemGroupData::getGroupName, name);//更具分组的名字查找
        lqw.eq(SystemGroupData::getStatus, CommonEnum.SHOW_STATUS_1.getValue());//查找开启的
        lqw.orderByDesc(SystemGroupData::getSort);//排序
        List<SystemGroupData> list = list(lqw);
//        List<SystemGroupDataVo> voList = BeanCopyUtils.copyBeanList(list, SystemGroupDataVo.class); 弃用，原因：将 SystemGroupData.value的属性由string转化成JSONObject
        //将数据封装成数组
        List data = new ArrayList<>();
        for (SystemGroupData i : list) {
            data.add(JSON.parse(i.getValue()));
        }
        return ResponseResult.okResult(data);
    }

    @Override
    public List<SystemGroupData> queryAll(SystemGroupDataQueryCriteria criteria) {
        LambdaQueryWrapper<SystemGroupData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemGroupData::getGroupName, criteria.getGroupName());
        return list(wrapper);
    }
}

